package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.PropEquityVestIn;
import com.sc.nft.entity.UserEquityProps;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.VistInFlagUserDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.UserEquityPropsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户权益道具表(sc_user_equity_props)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-03-28 10:28:43
 */
@Slf4j
@Repository
public class UserEquityPropsDao extends ServiceImpl<UserEquityPropsMapper, UserEquityProps> {
    @Resource
    private EquityPropsDao equityPropsDao;
    @Resource
    private UserInfoDao userInfoDao;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Resource
    private StaticDataDao staticDataDao;
    @Resource
    private PropEquityVestInDao propEquityVestInDao;


    public Page<MallEquityUserHoldVo> pageEquityHold(Page<UserEquityProps> page, List<Long> equityIds, Long userId) {
        return baseMapper.pageEquityHold(page, equityIds, userId);
    }

    public Page<MallUserEquityPropsVO> pageUserEquityProps(Long userId, Long vistIn, Integer status,
                                                           Page<UserEquityProps> page) {
        return baseMapper.pageUserEquityProps(userId, vistIn, status, page);
    }

    public Integer getHavingCountByUserId(Long equityPropsId, Long userId) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.gt(UserEquityProps::getExpiredTime, DateTime.now());
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        return this.count(wrapper);
    }

    public Integer updateHavingToShopSell(Long equityPropsId, Long userId, Integer count) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.gt(UserEquityProps::getExpiredTime, DateTime.now());
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        wrapper.set(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.SHOP_SELL);
        wrapper.set(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.ON_CONSIGN);
        wrapper.last(" limit " + count);
        return baseMapper.update(new UserEquityProps(), wrapper);
    }

    public Integer updateShopSellToHaving(Long equityPropsId, Long userId, Integer count) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.SHOP_SELL);
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.ON_CONSIGN);
        wrapper.set(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.set(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        wrapper.last(" limit " + count);
        return baseMapper.update(new UserEquityProps(), wrapper);
    }


    public Integer getHavingCountByUserId(Long equityPropsId, EquityPropsTypeEnum equityPropsType, Long userId) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getTypeClassify, equityPropsType);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.gt(UserEquityProps::getExpiredTime, DateTime.now());
        return this.count(wrapper);
    }

    public boolean updateConsignToOnConsign(Long id) {
        return lambdaUpdate().set(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.ON_CONSIGN).eq(UserEquityProps::getId, id).update();
    }

    /**
     * 查询用户持仓各类道具个数
     *
     * @param equityPropsType
     * @param userId
     * @return
     */
    public Integer getHavingEquityPropsCountByUserId(EquityPropsTypeEnum equityPropsType, Long userId) {
        return baseMapper.getHavingEquityPropsCountByUserId(userId);
    }

    public List<UserEquityProps> getHavingListByUserIdAndEquityPropsIdAndLimit(Long equityPropsId, Long userId, Integer size) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.gt(UserEquityProps::getExpiredTime, DateTime.now());
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        wrapper.orderByAsc(UserEquityProps::getExpiredTime);
        wrapper.last("limit " + size);
        return list(wrapper);
    }

    public List<UserEquityProps> getOnSellListByUserIdAndEquityPropsIdAndLimit(Long equityPropsId, Long userId, Integer size) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.SHOP_SELL);
        wrapper.gt(UserEquityProps::getExpiredTime, DateTime.now());
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.ON_CONSIGN);
        wrapper.last("limit " + size);
        return list(wrapper);
    }

    public List<UserEquityProps> getListByUserIdAndIds(Long userId, String ids, UserEquityPropsHoldingStatusEnum holdingStatus) {
        List<Long> collect = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());

        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserEquityProps::getId, collect);
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getHoldingStatus, holdingStatus);
        return list(wrapper);
    }


    public List<UserEquityProps> getVoucherList(Long equityPropsId, Long userId, EquityPropsTypeEnum equityPropsType, int num) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(equityPropsType != null, UserEquityProps::getTypeClassify, equityPropsType);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.ge(UserEquityProps::getExpiredTime, DateTime.now());
        wrapper.orderByAsc(UserEquityProps::getExpiredTime);
        wrapper.last("limit " + num);
        return list(wrapper);
    }

    public List<UserEquityProps> getVoucherList(Long equityPropsId, Long userId, int num, EquityPropsTypeEnum... equityPropsType) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.in(UserEquityProps::getTypeClassify, equityPropsType);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        wrapper.ge(UserEquityProps::getExpiredTime, DateTime.now());
        wrapper.orderByAsc(UserEquityProps::getExpiredTime);
        wrapper.last("limit " + num);
        return list(wrapper);
    }


    public Boolean insertBatchByList(ArrayList<UserEquityProps> list) {
        return this.saveBatch(list);
    }

    public Page<PageUserEquityPropsVO> getPageUserEquityProps(Page page, Long userId, Integer isExpired) {
        return baseMapper.getPageUserEquityProps(page, userId, isExpired);
    }


    public Page<UserEquityPropsVO> getMyEquity(Long userId, Long topId, Integer pageNo, Integer pageSize) {
        return baseMapper.getMyEquity(new Page(pageNo, pageSize), userId, topId);
    }

    public UserEquityPropsVO getMyEquityCount(Long activityId, Long userId, Long eqId) {
        return baseMapper.getMyEquityCount(activityId, userId, eqId);
    }

    public Page<UserEquityPropsDetailVO> getMyEquityDetail(Long userId, Long id, Integer isExpired, Page page, Boolean isGift) {
        Date dateTime = null;
        if (isGift == null) {
            isGift = Boolean.FALSE;
        }
        EquityProps byId = equityPropsDao.getById(id);
        if (isGift) {
            if (byId.getAging() != null) {
                dateTime = DateUtil.offsetDay(new Date(), byId.getAging());
            } else {
                dateTime = DateUtil.offsetDay(new Date(), 7);
            }
        }
        Boolean isSpecialGive;
        if (byId.getIsGift()) {
            UserInfo userInfo = userInfoDao.getById(userId);
            if (Objects.nonNull(userInfo) && userInfo.getGiftPermission()) {
                isSpecialGive = Boolean.TRUE;
            } else {
                isSpecialGive = Boolean.FALSE;
            }
        } else {
            isSpecialGive = Boolean.FALSE;
        }
        Page<UserEquityPropsDetailVO> myEquityDetail = baseMapper.getMyEquityDetail(page, userId, id, isExpired, isGift, dateTime);
        if (Objects.nonNull(myEquityDetail) && !CollectionUtil.isEmpty(myEquityDetail.getRecords())) {
            myEquityDetail.getRecords().forEach(e -> e.setIsSpecialGive(isSpecialGive));
        }
        return myEquityDetail;
    }


    /**
     * 统计奖券发放
     *
     * @param propId    道具id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param status    发放什么类型的奖券.
     * @return
     */
    public Integer lotteryTicketDistributionStatistics(Long propId, Date startTime, Date endTime, Integer status) {
        LambdaQueryWrapper<UserEquityProps> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserEquityProps::getEquityPropsId, propId);
        lambdaQueryWrapper.between(UserEquityProps::getCreateTime, startTime, endTime);
        lambdaQueryWrapper.eq(status != null, UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.CONSUME);
        return count(lambdaQueryWrapper);
    }

    public Integer lotteryTicketDistributionStatisticsEmploy(Long propId, Date startTime, Date endTime, Integer status) {
        LambdaQueryWrapper<UserEquityProps> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserEquityProps::getEquityPropsId, propId);
        lambdaQueryWrapper.between(UserEquityProps::getUseTime, startTime, endTime);
        lambdaQueryWrapper.eq(status != null, UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.CONSUME);
        return count(lambdaQueryWrapper);
    }


    public List<UserEquityProps> getUserIdAndPropIdsAndTypeEnumAndStatusEnum(Long userId, List<Long> propIds, EquityPropsTypeEnum equityPropsTypeEnum, UserEquityPropsHoldingStatusEnum userEquityPropsHoldingStatusEnum) {
        if (CollUtil.isEmpty(propIds)) {
            return new ArrayList<>();
        }
        return lambdaQuery().eq(UserEquityProps::getUserId, userId).eq(UserEquityProps::getEquityPropsId, propIds).eq(UserEquityProps::getTypeClassify, equityPropsTypeEnum).eq(UserEquityProps::getHoldingStatus, userEquityPropsHoldingStatusEnum).gt(UserEquityProps::getExpiredTime, DateTime.now()).list();
    }

    public List<UserEquityProps> getUserIdAndIdsAndTypeEnumAndStatusEnum(Long userId, List<Long> id, UserEquityPropsHoldingStatusEnum userEquityPropsHoldingStatusEnum, EquityPropsTypeEnum... equityPropsTypeEnum) {
        if (CollUtil.isEmpty(id)) {
            return new ArrayList<>();
        }
        return lambdaQuery().eq(UserEquityProps::getUserId, userId).in(UserEquityProps::getId, id).in(UserEquityProps::getTypeClassify, equityPropsTypeEnum).eq(UserEquityProps::getHoldingStatus, userEquityPropsHoldingStatusEnum).gt(UserEquityProps::getExpiredTime, DateTime.now()).list();
    }

    public List<UserEquityProps> getUserIdAndPropIdsAndTypeEnumAndStatusEnum(Long userId, List<Long> propIds, UserEquityPropsHoldingStatusEnum userEquityPropsHoldingStatusEnum, EquityPropsTypeEnum... equityPropsTypeEnum) {
        if (CollUtil.isEmpty(propIds)) {
            return new ArrayList<>();
        }
        return lambdaQuery().eq(UserEquityProps::getUserId, userId).in(UserEquityProps::getEquityPropsId, propIds)
                .in(UserEquityProps::getTypeClassify, equityPropsTypeEnum).
                eq(UserEquityProps::getHoldingStatus, userEquityPropsHoldingStatusEnum).
                eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN).
                gt(UserEquityProps::getExpiredTime, DateTime.now()).list();
    }

    public List<UserEquityProps> getUserIdAndIdsAndTypeEnumAndStatusEnum(Long userId, List<Long> ids, EquityPropsTypeEnum equityPropsTypeEnum, UserEquityPropsHoldingStatusEnum userEquityPropsHoldingStatusEnum) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return lambdaQuery().eq(UserEquityProps::getUserId, userId).in(UserEquityProps::getId, ids).eq(UserEquityProps::getTypeClassify, equityPropsTypeEnum).eq(UserEquityProps::getHoldingStatus, userEquityPropsHoldingStatusEnum).gt(UserEquityProps::getExpiredTime, DateTime.now()).list();
    }


    public void gift(List<Long> ids) {
        lambdaUpdate().in(UserEquityProps::getId, ids).set(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.GIFT).set(UserEquityProps::getGetType, UserEquityPropsGetTypeEnum.GIFT).update();
    }

    public Page<UserEquityPropsSimpleVO> pageUserWithEquityProps(Long userId, String firstTitle, int pageNo, int pageSize) {
        return baseMapper.pageUserWithEquityProps(new Page<>(pageNo, pageSize), userId, firstTitle);
    }

    public Integer getHasUserCountByEquityId(Long equityId) {
        return baseMapper.getHasUserCountByEquityId(equityId);
    }

    public Integer getSumCountByEquityId(Long equityId) {
        LambdaQueryWrapper<UserEquityProps> query = new LambdaQueryWrapper<>();
        query.eq(UserEquityProps::getIsDelete, Boolean.FALSE);
        query.eq(UserEquityProps::getEquityPropsId, equityId);
        return count(query);
    }

    public Integer getCanUseCountByEquityId(Long equityId) {
        LambdaQueryWrapper<UserEquityProps> query = new LambdaQueryWrapper<>();
        query.eq(UserEquityProps::getIsDelete, Boolean.FALSE);
        query.eq(UserEquityProps::getEquityPropsId, equityId);
        query.in(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL, UserEquityPropsHoldingStatusEnum.FREEZE);
        return count(query);
    }

    public Integer getCanUseCountByEquityIdAndUserId(Long equityId, Long userId) {
        LambdaQueryWrapper<UserEquityProps> query = new LambdaQueryWrapper<>();
        query.eq(UserEquityProps::getUserId, userId);
        query.eq(UserEquityProps::getEquityPropsId, equityId);
        query.in(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL, UserEquityPropsHoldingStatusEnum.FREEZE, UserEquityPropsHoldingStatusEnum.SHOP_SELL);
        return count(query);
    }

    public Page<EquityPropsExpiredLogPageVO> expiredLog(Long userId, String firstTitle, Date startTime, Date endTime, Integer pageNo, Integer pageSize) {
        return baseMapper.expiredLog(new Page<>(pageNo, pageSize), userId, firstTitle, startTime, endTime);
    }

    public Page<AdminUserEquityPropsPagVO> getAdminUserEquityPropsPage(Page page, String userTel, String firstTitle, Date getBeginTime, Date getEndTime, Date expiredBeginTime, Date expiredEndTime, Integer onConsignStatus) {
        return baseMapper.getAdminUserEquityPropsPage(page, userTel, firstTitle, getBeginTime, getEndTime, expiredBeginTime, expiredEndTime, onConsignStatus);
    }

    public Integer getCountByIdsAndHoldStats(List<Long> ids, UserEquityPropsHoldingStatusEnum userEquityPropsHoldingStatusEnum) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getHoldingStatus, userEquityPropsHoldingStatusEnum);
        wrapper.in(UserEquityProps::getId, ids);
        return count(wrapper);
    }

    public boolean batchEditExpiredTime(List<Long> ids, Date expiredTime) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(UserEquityProps::getId, ids);
        wrapper.lt(UserEquityProps::getCreateTime, expiredTime);
        wrapper.set(UserEquityProps::getExpiredTime, expiredTime);
        return update(new UserEquityProps(), wrapper);
    }

    public Page<UserConsignEquityPropsPageVO> getUserConsignEquityPropsPage(Page page, Long equityPropsId, Long userId) {
        return baseMapper.getUserConsignEquityPropsPage(page, equityPropsId, userId);
    }

    /**
     * 查询传入的权益道具是否可以寄售
     * 1.到期时间大于等于365天
     * 2.是正常的持有状态
     *
     * @param userId
     * @param equityPropsId
     * @param userEquityPropsId
     * @return
     */
    public Integer getSellableCountByUserIdAndEquityPropsIdAndIds(Long userId, Long equityPropsId, List<Long> userEquityPropsId) {
        return baseMapper.getSellableCountByUserIdAndEquityPropsIdAndIds(userId, equityPropsId, userEquityPropsId);
    }

    public boolean updateOnConsignAById(Long id, UserEquityConsignStatusEnum onConsign) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEquityProps::getId, id);
        wrapper.set(UserEquityProps::getOnConsign, onConsign);
        return update(new UserEquityProps(), wrapper);
    }

    /**
     * 获取道具是否在寄售中
     *
     * @param userId
     * @param userEquityPropId
     */

    public boolean cancelConsignByUserEquityPropId(Long userId, Long userEquityPropId, UserEquityConsignStatusEnum onConsign) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<UserEquityProps>().eq(UserEquityProps::getId, userEquityPropId).eq(UserEquityProps::getUserId, userId).eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL).set(UserEquityProps::getOnConsign, onConsign);

        return update(new UserEquityProps(), wrapper);
    }


    public boolean updateToSales(Long id, Long orderId, Long salesUserId, Long buyUserId, Date payTime) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<UserEquityProps>();
        wrapper.eq(UserEquityProps::getId, id);
        wrapper.eq(UserEquityProps::getUserId, salesUserId);
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.ON_CONSIGN);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.set(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.SALES);
        wrapper.set(UserEquityProps::getSecondOrderId, orderId);
        wrapper.set(UserEquityProps::getToUserId, buyUserId);
        wrapper.set(UserEquityProps::getTransferTime, payTime);
        return update(wrapper);
    }

    public boolean updateWantSellToSales(Long id, Long orderId, Long salesUserId, Long buyUserId, Date payTime) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<UserEquityProps>();
        wrapper.eq(UserEquityProps::getId, id);
        wrapper.eq(UserEquityProps::getUserId, salesUserId);
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.CONSIGN_FREEZE);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.SHOP_SELL);
        wrapper.set(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.SALES);
        wrapper.set(UserEquityProps::getSecondOrderId, orderId);
        wrapper.set(UserEquityProps::getToUserId, buyUserId);
        wrapper.set(UserEquityProps::getTransferTime, payTime);
        return update(wrapper);
    }

    public boolean updateWantBuyOrderToSales(Long id, Long orderId, Long salesUserId, Long buyUserId, Date payTime) {
        LambdaUpdateWrapper<UserEquityProps> wrapper = new LambdaUpdateWrapper<UserEquityProps>();
        wrapper.eq(UserEquityProps::getId, id);
        wrapper.eq(UserEquityProps::getUserId, salesUserId);
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.set(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.WANT_BUY_CONSUME);
        wrapper.set(UserEquityProps::getSecondOrderId, orderId);
        wrapper.set(UserEquityProps::getToUserId, buyUserId);
        wrapper.set(UserEquityProps::getTransferTime, payTime);
        return update(wrapper);
    }

    public Boolean getIsHoldEquityDetail(Long userId, Long equityPropsId) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.gt(UserEquityProps::getExpiredTime, DateTime.now());
        return count(wrapper) > 0;
    }

    public List<HoldPropDataVO> holdProp(Long userId, String propName) {
        return baseMapper.holdProp(userId, propName);
    }

    public List<HoldPropDataVO> getSingleHoldEquityPropDataByUserIdAndEquityPropId(Long userId, Long id) {
        return baseMapper.getSingleHoldEquityPropDataByUserIdAndEquityPropId(userId, id);
    }

    public Page<HoldPropDataVO> getSingleHoldEquityPropData(Long userId, Integer pageNo, Integer pageSize, Long id) {
        return baseMapper.getSingleHoldEquityPropData(new Page(pageNo, pageSize), userId, id);
    }

    public List<UserHoldPropsCountVO> getUserPropsHoldCount(Long userId, List<Long> equityPropsIds) {
        return baseMapper.getUserPropsHoldCount(userId, equityPropsIds);
    }


    public List<UserEquityProps> getListByUserIdAndEquityId(Long equityId, Long userId, Integer limit) {
        LambdaQueryWrapper<UserEquityProps> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEquityProps::getEquityPropsId, equityId);
        queryWrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        queryWrapper.gt(UserEquityProps::getExpiredTime, new Date());
        queryWrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        queryWrapper.eq(UserEquityProps::getUserId, userId);
        queryWrapper.last(" limit " + limit);
        return list(queryWrapper);
    }


    public int updateSession(List<UserEquityProps> list, Long activityId) {
        SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        try {
            // 获取批量操作的新的map
            UserEquityPropsMapper mapper = batchSqlSession.getMapper(UserEquityPropsMapper.class);

            for (UserEquityProps userEquityProps : list) {
                mapper.updateUserEquityById(activityId, userEquityProps.getId());
            }
            // session 提交
            batchSqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            batchSqlSession.rollback();
        } finally {
            // session 关闭
            batchSqlSession.close();
        }
        return list.size();
    }

    /**
     * 根据补签道具Id 获取用户补签道具有多少
     *
     * @param userId
     * @param equityPropsId
     * @return
     */
    public Integer getMyEquityPropsByEquityPropsId(Long userId, Long equityPropsId) {
        return baseMapper.getMyEquityPropsByEquityPropsId(userId, equityPropsId);
    }

    public List<UserEquityProps> getListByIds(List<Long> ids) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserEquityProps::getId, ids);
        return list(wrapper);
    }

    public List<UserEquityProps> getListByIdsAndDelete(List<Long> userEquityPropsIds) {
        return baseMapper.getListByIdsAndDelete(userEquityPropsIds);
    }

    public Boolean updateHoldStatusBatchById(List<Long> ids, UserEquityPropsHoldingStatusEnum statusEnum, UserEquityPropsHoldingStatusEnum beforeStatusEnum) {
        LambdaUpdateWrapper<UserEquityProps> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(UserEquityProps::getId, ids);
        updateWrapper.eq(UserEquityProps::getHoldingStatus, beforeStatusEnum);
        updateWrapper.set(UserEquityProps::getHoldingStatus, statusEnum);
        return update(updateWrapper);
    }

    public Boolean updateByIds(List<Long> userEquityPropsIdList, UserEquityPropsHoldingStatusEnum userEquityPropsHoldingStatusEnum, Date now, UserEquityPropsUseTypeEnum userEquityPropsUseTypeEnum) {
        LambdaUpdateWrapper<UserEquityProps> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserEquityProps::getHoldingStatus, userEquityPropsHoldingStatusEnum);
        updateWrapper.set(UserEquityProps::getUseTime, now);
        updateWrapper.set(UserEquityProps::getUseType, userEquityPropsUseTypeEnum);
        updateWrapper.in(UserEquityProps::getId, userEquityPropsIdList);
        return update(updateWrapper);
    }

    /**
     * 商城用户持有的权益道具
     *
     * @param vistInFlagUserDTO
     * @return
     */
    public List<MallEquityPropsVO> getUserEquityProps(VistInFlagUserDTO vistInFlagUserDTO) {
        PropEquityVestIn propEquityVestIn = propEquityVestInDao.getFlag(vistInFlagUserDTO.getFlag());
        if (BeanUtil.isEmpty(propEquityVestIn)) {
            throw new GlobalRunTimeException("无效的访问标识");
        }
        return baseMapper.getUserEquityProps(propEquityVestIn.getId(), vistInFlagUserDTO.getUserId());
    }

    public Boolean updateFromStatusToStatus(Long id, UserEquityPropsHoldingStatusEnum fromStatus, UserEquityPropsHoldingStatusEnum toStatus, UserEquityConsignStatusEnum onConsign) {
        LambdaUpdateWrapper<UserEquityProps> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserEquityProps::getHoldingStatus, toStatus);
        updateWrapper.eq(UserEquityProps::getId, id);
        updateWrapper.eq(UserEquityProps::getHoldingStatus, fromStatus);
        updateWrapper.eq(UserEquityProps::getOnConsign, onConsign);
        return update(updateWrapper);
    }

    public IPage<UserEquityProps> havingByUserIdPage(Page page, Long equityPropsId, Long userId) {
        LambdaQueryWrapper<UserEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEquityProps::getUserId, userId);
        wrapper.eq(UserEquityProps::getEquityPropsId, equityPropsId);
        wrapper.eq(UserEquityProps::getHoldingStatus, UserEquityPropsHoldingStatusEnum.NORMAL);
        wrapper.gt(UserEquityProps::getExpiredTime, DateTime.now());
        wrapper.eq(UserEquityProps::getOnConsign, UserEquityConsignStatusEnum.NOT_CONSIGN);
        wrapper.orderByAsc(UserEquityProps::getExpiredTime);
        return page(page, wrapper);
    }

}